package com.future.mayi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.utils.ToastUtil;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.KefuModel;
import com.future.mayi.model.KefuWxModel;
import com.future.mayi.response.KefuResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class KefuDialog extends Dialog {

    @BindView(R.id.iv_wx)
    ImageView ivWx;
    @BindView(R.id.tv_wx)
    TextView tvWx;
//    @BindView(R.id.tv_title)
//    TextView tvTitle;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    private Context context;
    private static KefuModel kefuModel;

    public KefuDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        this.context = context;
        View layout = LayoutInflater.from(context).inflate(R.layout.dialog_kefu, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (CommonUtils.getScreenSize(context)[0] * 0.75f), ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(layout, params);
        ButterKnife.bind(this);


    }

    private KefuWxModel model;

    private int showType = 0;


    public void show(int showType) {
        this.showType = showType;
        super.show();
        if (ivWx == null) return;
        if (kefuModel == null) {
            String kefu = (String) SharedPreferencesUtils.get(context, SharedPreferencesUtils.SpEnum.Kefu, "");
            if (!TextUtils.isEmpty(kefu)) {
                KefuModel model = App.getGson().fromJson(kefu, KefuModel.class);
                if (model != null) {
                    kefuModel = model;
                    show(showType);
                }
            }
            getKfwx();
        } else if (showType == 0) {
            model = kefuModel.getWeixin();
            if (model == null) return;
            HCUtils.loadWebImg(context, ivWx, model.getQrcode());
            tvWx.setText("微信号：" + model.getWx_number());
//            tvTitle.setText("售前客服");
        } else {
            model = kefuModel.getShouhou();
            if (model == null) return;
            HCUtils.loadWebImg(context, ivWx, model.getQrcode());
            tvWx.setText("微信号：" + model.getWx_number());
//            tvTitle.setText("售后客服");
        }

    }


    @OnClick({R.id.iv_close, R.id.tv_enter})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_enter:
                if (model == null) return;
                CommonUtils.clipData(context, model.getWx_number());
                ToastUtil.getInstance().show(context, "复制成功");
                dismiss();
                break;
        }
    }


    private void getKfwx() {
        new HttpRequest<KefuResponse>()
                .with(context)
                //.addParam("type", showType)
                .setApiCode(ApiCst.getKfrx)
                .setListener(new HttpRequest.OnNetworkListener<KefuResponse>() {
                    @Override
                    public void onSuccess(KefuResponse response) {
                        if (response.models == null) return;
                        SharedPreferencesUtils.set(context, SharedPreferencesUtils.SpEnum.Kefu, response.content);
                        kefuModel = response.models;
                        show(showType);
                    }

                    @Override
                    public void onFail(String message, KefuResponse response) {
                        ToastUtil.getInstance().show(context, message);
                    }
                })
                .start(new KefuResponse());
    }
}
